package com.mall.web.controller.product;

import com.mall.product.domain.entity.ProCategory;
import com.mall.system.domain.entity.SysMenu;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mall.common.annotation.Log;
import com.mall.common.enums.BusinessType;
import com.mall.product.service.ProCategoryService;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.common.utils.StringUtils;
import com.mall.common.core.domain.Ztree;

/**
 * 商品分类Controller
 *
 * @author zhonglin
 * @date 2020-04-28
 */
@Controller
@RequestMapping("/product/category")
public class ProCategoryController
    extends BaseController {

  private String prefix = "product/category";

  @Autowired
  private ProCategoryService proCategoryService;

  @RequiresPermissions("product:category:view")
  @GetMapping()
  public String category() {

    return prefix + "/category";
  }

  /**
   * 查询商品分类树列表
   */
  @RequiresPermissions("product:category:list")
  @PostMapping("/list")
  @ResponseBody
  public List<ProCategory> list(ProCategory proCategory) {

    List<ProCategory> list = proCategoryService.selectProCategoryList(proCategory);
    return list;
  }

  /**
   * 导出商品分类列表
   */
  @RequiresPermissions("product:category:export")
  @Log(title = "商品分类", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(ProCategory proCategory) {

    List<ProCategory>      list = proCategoryService.selectProCategoryList(proCategory);
    ExcelUtil<ProCategory> util = new ExcelUtil<ProCategory>(ProCategory.class);
    return util.exportExcel(list, "category");
  }

  /**
   * 新增商品分类
   */
  @GetMapping("/add/{parentId}")
  public String add(@PathVariable(value = "parentId") Long parentId,
                    ModelMap mmap) {

    ProCategory category = null;
    if (0L != parentId) {
      category = proCategoryService.selectProCategoryById(parentId);
    } else {
      category = new ProCategory();
      category.setCategoryId(0L);
//            category.setCategoryName("主目录");
    }
    mmap.put("category", category);
    return prefix + "/add";
  }

  /**
   * 新增保存商品分类
   */
  @RequiresPermissions("product:category:add")
  @Log(title = "商品分类", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(ProCategory proCategory) {

    return toAjax(proCategoryService.insertProCategory(proCategory));
  }

  /**
   * 修改商品分类
   */
  @GetMapping("/edit/{categoryId}")
  public String edit(@PathVariable("categoryId") Long categoryId,
                     ModelMap mmap) {

    ProCategory proCategory = proCategoryService.selectProCategoryById(categoryId);
    mmap.put("proCategory", proCategory);
    return prefix + "/edit";
  }

  /**
   * 修改保存商品分类
   */
  @RequiresPermissions("product:category:edit")
  @Log(title = "商品分类", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(ProCategory proCategory) {

    return toAjax(proCategoryService.updateProCategory(proCategory));
  }

  /**
   * 删除
   */
  @RequiresPermissions("product:category:remove")
  @Log(title = "商品分类", businessType = BusinessType.DELETE)
  @GetMapping("/remove/{categoryId}")
  @ResponseBody
  public AjaxResult remove(@PathVariable("categoryId") Long categoryId) {

    return toAjax(proCategoryService.deleteProCategoryById(categoryId));
  }

  /**
   * 选择商品分类树
   */
  @GetMapping(value = {"/selectCategoryTree/{categoryId}", "/selectCategoryTree/"})
  public String selectCategoryTree(@PathVariable(value = "categoryId", required = false) Long categoryId,
                                   ModelMap mmap) {

    if (StringUtils.isNotNull(categoryId)) {
      mmap.put("proCategory", proCategoryService.selectProCategoryById(categoryId));
    }
    return prefix + "/tree";
  }

  /**
   * 加载商品分类树列表
   */
  @GetMapping("/treeData")
  @ResponseBody
  public List<Ztree> treeData() {

    List<Ztree> ztrees = proCategoryService.selectProCategoryTree();
    return ztrees;
  }

}
